#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QTime>
#include <QDateTime>
#include <QMessageBox>
#include <QKeySequence>

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);
    Init_UI();

    this->_nums=4;
    this->_tlabel_id=this->startTimer(1000);
}

LoginWidget::~LoginWidget()
{
    delete ui;
}


void LoginWidget::Init_UI(){

    this->setWindowTitle("Login");

    ui->stackedWidget->setCurrentIndex(0);

    connect(ui->login_enter_button,&QPushButton::clicked,this,&LoginWidget::Login_Enter_Button);
    connect(ui->login_pw_line,&QLineEdit::returnPressed,this,&LoginWidget::Login_Enter_Button);
    connect(ui->login_enter2_button,&QPushButton::clicked,this,&LoginWidget::Login_Enter2_Button);
    connect(ui->login_premission_line,&QLineEdit::returnPressed,this,&LoginWidget::Login_Enter2_Button);
    connect(ui->login_cancle_button,&QPushButton::clicked,[this](){
        emit this->LoginFault();
    });

    connect(ui->login_cancle2_button,&QPushButton::clicked,[this](){
        emit this->LoginFault();
    });

//    ui->login_enter_button->setShortcutEnabled(Qt::Key_Enter,true);

    ui->login_enter_button->setShortcut(Qt::Key::Key_Enter);

    ui->login_level_combobox->addItems({"公开","私密"});

    ui->login_pw_line->setPlaceholderText("root密码");

    ui->login_pw_line->setEchoMode(QLineEdit::Password);

}

void LoginWidget::timerEvent(QTimerEvent *tevent){

    if(this->_tlabel_id==tevent->timerId()){
        QString time=QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
        ui->login_time_label->setText(time);
    }
}

/*void LoginWidget::Login_Enter_Button(){
    if(ui->login_pw_line->text().isEmpty()){
        QMessageBox::warning(this,"password","密码不能为空");
    }else{
        Yzty::ROOTPW=ui->login_pw_line->text();
        if(Yzty::VerifyRootPW(Yzty::ROOTPW)){
            ui->stackedWidget->setCurrentIndex(1);
            this->_nums=5;
            emit LoginFinish_1();
        }else
            QMessageBox::warning(this,"password",QString("密码错误,"
                                                         "还有%1次机会").arg(--this->_nums));
    }
}*/

void LoginWidget::Login_Enter_Button(){
    if(ui->login_pw_line->text().isEmpty()){
        QMessageBox::warning(this,"password","密码不能为空");
    }else{
        Yzty::ROOTPW=ui->login_pw_line->text();
        if(Yzty::VerifyRootPW(Yzty::ROOTPW)){
            ui->stackedWidget->setCurrentIndex(1);
            this->_nums=4;
            emit LoginFinish_1();
        }else if(this->_nums >0){
            QMessageBox::warning(this,"password",QString("密码错误,"
                                                         "还有%1次机会").arg(this->_nums--));
        }else{
            emit LoginError();
        }
    }
}

/*void LoginWidget::Login_Enter2_Button(){
    this->_nums = 5;
    if(ui->login_premission_line->text().isEmpty()){
        QMessageBox::warning(this,"premission","动态口令不能为空");
    }else{
        Yzty::PREMISSPN=ui->login_premission_line->text();
        if(Yzty::VerifyPerMission(Yzty::PREMISSPN)){
            Yzty::LEVEL=ui->login_level_combobox->currentIndex();
            emit LoginFinish();
        }
        else
            QMessageBox::warning(this,"premission",QString("动态口令错误,"
                                                           "还有%1次机会").arg(--this->_nums));
    }
}*/
void LoginWidget::Login_Enter2_Button(){
    if(ui->login_premission_line->text().isEmpty()){
        QMessageBox::warning(this,"premission","动态口令不能为空");
    }else{
        Yzty::PREMISSPN=ui->login_premission_line->text();
        if(Yzty::VerifyPerMission(Yzty::PREMISSPN)){
            Yzty::LEVEL=ui->login_level_combobox->currentIndex();
            emit LoginFinish();
        }else if(this->_nums >0){
            QMessageBox::warning(this,"premission",QString("动态口令错误，还有1%次机会").arg(this->_nums--));
        }else{
            emit LoginError();
        }
    }
}
